<?php
	require_once ("../connectDb.php");
	class admin extends ConnectDb
	{
		//Them loai tin
		public function InsertSubCat()
		{
			$idTL=$_POST['idTL'];
			$lang=$_POST['lang'];
			$Ten=$_POST['NameSubCat'];
			$ThuTu=$_POST['Order'];
			$AnHien=$_POST['Active'];
			
			settype($idTL,"int");
			settype($ThuTu,"int");
			settype($AnHien,"int");
			$Ten=trim(strip_tags($Ten));
			$lang=trim(strip_tags($lang));
			if(get_magic_quotes_gpc()==false) $Ten=mysql_real_escape_string($Ten);
			if(get_magic_quotes_gpc()==false) $lang=mysql_real_escape_string($lang);
			
			$query="INSERT INTO loaitin (idTL,lang,Ten,ThuTu,AnHien)
					VALUES ($idTL,'$lang','$Ten',$ThuTu,$AnHien)";
			mysql_query($query) or die(mysql_error());
		}
		//Xem loai tin
		public function ListSubCat($idTL=-1)
		{
			settype($idTL,"int");
			$query="SELECT idLT,Ten,loaitin.AnHien,loaitin.ThuTu,TenTL
					FROM loaitin,theloai
					WHERE (loaitin.idTL=theloai.idTL) AND (loaitin.idTL='$idTL' OR $idTL=-1)
					ORDER BY loaitin.ThuTu ASC
					LIMIT 0,5";
			$result=mysql_query($query) or die(mysql_error());
			return $result;
		}
		//Xoa loai tin
		public function DelSubCat($idLT)
		{
			settype($idLT,"int");
			$query="DELETE FROM loaitin WHERE idLT='$idLT'";
			mysql_query($query) or die(mysql_error());
		}
		//Chi tiet loai tin
		public function DetailSubCat($idLT)
		{
			settype($idLT,"int");
			$query="SELECT * FROM loaitin WHERE idLT='$idLT'";
			$result=mysql_query($query) or die(mysql_error());
			return $result;
		}
		//Update loai tin
		public function UpdateSubCat($idLT)
		{
			$idTL=$_POST['idTL'];
			$Ten=$_POST['nameSubCat'];
			$ThuTu=$_POST['order'];
			$AnHien=$_POST['AnHien'];
			
			settype($idTL,"int");
			settype($ThuTu,"int");
			settype($AnHien,"int");
			$Ten=trim(strip_tags($Ten));
			if(get_magic_quotes_gpc()==false) $Ten=mysql_real_escape_string($Ten);
			
			$query="UPDATE loaitin
					SET Ten='$Ten', ThuTu='$ThuTu', AnHien='$AnHien', idTL='$idTL'
					WHERE idLT='$idLT'";
			mysql_query($query) or die(mysql_error());
		}
		/*---------------------------------End SubCat--------------------------------------------------------------------------- */
		//Lay tin
		public function ListNews()
		{
			$query="SELECT idTin,TieuDe,TomTat,Ngay,SoLanXem,tin.AnHien,TinNoiBat,TenTL,Ten
					FROM tin,theloai,loaitin
					WHERE (tin.idTL=theloai.idTL) AND (tin.idLT=loaitin.idLT)
					ORDER BY idTin DESC
					LIMIT 0,10";
			$result=mysql_query($query) or die(mysql_error());
			return $result;
		}
		//Xoa tin
		public function DeleteNews($idTin)
		{
			settype($idTin,"int");
			if($idTin<=0) return;
			$query="DELETE FROM tin WHERE idTin='$idTin'";
			mysql_query($query) or die(mysql_error());
		}
		//Chinh tin
		public function UpdateNews($idTin)
		{
			$lang=$_POST['lang'];
			$TieuDe=$_POST['TieuDe'];
			$TomTat=$_POST['TomTat'];
			$urlHinh=$_POST['urlHinh'];
			$Ngay=$_POST['Ngay'];
			$AnHien=$_POST['AnHien'];
			$TinNoiBat=$_POST['TinNoiBat'];
			$idTL=$_POST['idTL'];
			$idLT=$_POST['idLT'];
			$Content=$_POST['Content'];
			
			settype($AnHien,"int");
			settype($idTin,"int");
			settype($idLT,"int");
			settype($idTL,"int");
			$lang=trim(strip_tags($lang));
			$TieuDe=trim(strip_tags($TieuDe));
			$TomTat=trim(strip_tags($TomTat));
			$urlHinh=trim(strip_tags($urlHinh));
			$Content=trim(strip_tags($Content));
			$Ngay=trim(strip_tags($Ngay));
			if(get_magic_quotes_gpc()==false)
			{
				$lang = mysql_real_escape_string($lang);
				$TieuDe = mysql_real_escape_string($TieuDe);
				$TomTat = mysql_real_escape_string($TomTat);
				$Content= mysql_real_escape_string($Content);
				$urlHinh = mysql_real_escape_string($urlHinh);
				$Ngay = mysql_real_escape_string($Ngay);
				
			}
			$Ngay_arr=explode("/",$Ngay);
			if(count($Ngay_arr)==3)
			{
				$d=$Ngay_arr[0];
				$m=$Ngay_arr[1];
				$y=$Ngay_arr[2];
				if(checkdate($m,$d,$y)==false) $Ngay=date("Y-m-d");
				else $Ngay=$y."-".$m."-"."-".$d;
			}
			else $Ngay=date("Y-m-d");
			$query="UPDATE tin
					SET TieuDe='$TieuDe', TomTat='$TomTat', 
						urlHinh='$urlHinh', Ngay='$Ngay', lang='$lang', 
						idTL=$idTL, idLT=$idLT,	idUser=$idUser, 
						AnHien=$AnHien, TinNoiBat = $TinNoiBat, Content='$Content' 
					WHERE idTin=$idTin";
			mysql_query($query) or die(mysql_error());
		}
	}
?>